package hutoolTest;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class TreeUtilTest {
    public static void main(String[] args) {
        List<TreeNode<String>> nodeList = new CollUtil().newArrayList();
        nodeList.add(new TreeNode<>("1", "0", "分区管理员", 0));
        nodeList.add(new TreeNode<>("2", "0", "C区管理员", 0));
        nodeList.add(new TreeNode<>("3", "1", "小区管理员", 0));

        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
//        System.out.println(JSONObject.toJSONString(treeList));


        //配置版
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setWeightKey("order");
        List<Tree<String>> treeList2 = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    tree.putExtra("type", "1");
                });
        System.out.println(JSONObject.toJSONString(treeList2));
    }
}




















